
import { _decorator, Component, Node, RigidBody, Vec3, MeshCollider, MeshRenderer, BoxCollider, utils, primitives } from 'cc';

const { ccclass, property } = _decorator;

enum ThingState {
    None,
    Holdon,
    Moving
}

@ccclass('Thing')
export class Thing extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    private _rigidBody!: RigidBody
    private _status!: ThingState
    start() {
        this._status = ThingState.None
    }

    addPhysicsProperty() {
        this._rigidBody = this.node.addComponent(RigidBody)
        let comp_col = this.node.addComponent(MeshCollider)
        comp_col.convex = true
        let box = primitives.box()
        let mesh_phy = utils.createMesh(box)
        comp_col.mesh = this.node.children[0].getComponent(MeshRenderer)?.mesh!
        //v3.0 bug 在此临时修复 https://forum.cocos.org/t/topic/108432
        this.enabled = true
        let p = this.node.parent
        this.node.removeFromParent()
        p?.addChild(this.node)
    }

    holdOn(starpos: Vec3) {
        // if (this._status == ThingState.None) {
            console.log("hold on", starpos)
            this.node.setPosition(starpos)
            this._rigidBody.useGravity = false
            // this._status = ThingState.Holdon
        // }
    }

    holdMove(starpos: Vec3){
        // if (this._status == ThingState.Holdon||ThingState.Moving){
            this.node.setPosition(starpos)
        // }
    }
}
